import CommentUI from "./comment.UI";
import React, { Component } from "react";
import { messages } from "share/common";
import RequestService from "containers/request/request.service";
import httpFetch from "share/httpFetch";
import chooserData from "share/chooserData";

class CommentBS extends Component {
  state = {
    localProps: {},
    selectorItem: null,
  };
  componentWillMount() {
    let { type, selectorItem = {} } = chooserData;
    let chooserItem = {};
    if (type) {
      chooserItem = chooserData[type];
    }
    const result = Object.assign({}, chooserItem, selectorItem);
    this.setState({
      selectorItem: result,
      localProps: {
        labelKey: result.labelKey,
        valueKey: result.valueKey,
      },
    });
  }
  getService = (keyWord) => {
    console.log("getService", keyWord);
    let { extraParams = {}, service, url } = this.props;
    let { selectorItem } = this.state;
    let localService = null;
    let params = {
      keyword: keyWord,
      ...extraParams,
    };
    if (service) {
      localService = service(params);
    } else if (selectorItem && selectorItem.url) {
      localService = httpFetch.get(url, params);
    } else if (url) {
      if (typeof url === "function") {
        url = url();
      }
      localService = httpFetch.get(url, params);
    } else {
      localService = RequestService.getUser(params);
    }

    return localService;
  };

  render() {
    const { localProps } = this.state;
    const { visible } = this.props;
    if (!visible) return null;
    const props = Object.assign({}, localProps, this.props);
    return <CommentUI {...this.props} service={this.getService} />;
  }

  static propTypes = {
    visible: React.PropTypes.bool,
    value: React.PropTypes.oneOfType(React.PropTypes.string, React.PropTypes.object),
    onChange: React.PropTypes.func,
    placeholder: React.PropTypes.string,
    labelKey: React.PropTypes.string,
    labelListKeys: React.PropTypes.array, // 列表项要展示的数据格式
    valueKey: React.PropTypes.string,
    service: React.PropTypes.func, // 提供自定义的service，需配套提供 labelKey 和 ValueKey
    type: React.PropTypes.string,
    url: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.func]), // 支持get 及 keyWord的相关接口
    extraParams: React.PropTypes.object,
  };
  static defaultProps = {
    visible: false,
    onChange: () => {},
    placeholder: messages("common.please.input") /*请输入*/,
    url: "/api/users/v3/search",
    labelKey: "fullName",
    valueKey: "userOID",
    labelListKeys: ["employeeID", "fullName"],
    extraParams: {
      roleType: "TENANT",
      page: 0,
      size: 6,
    },
  };
}

export default CommentBS;
